package thread;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadLocalDemo {

    private ThreadLocal<SimpleDateFormat> sdf = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
    private ThreadLocal<Long> longThreadLocal = new ThreadLocal<>();

    public String date(int i) {
        return sdf.get().format(new Date(i * 1000));
    }

    public Long get() {
        return longThreadLocal.get();
    }

    public void setLong(Long l) {
        longThreadLocal.set(l);
    }

    public static void main(String[] args) {
//        ExecutorService executorService = Executors.newFixedThreadPool(10);
//        for (int i = 0; i < 10000; i++) {
//            final int t = i;
//            executorService.submit(() -> {
//                ThreadLocalDemo demo = new ThreadLocalDemo();
//                System.out.println(demo.date(t));
//            });
//        }
        ThreadLocalDemo demo = new ThreadLocalDemo();
        demo.setLong(1L);
        System.out.println(demo.get());
    }

}
